package offer_test_20;

import java.util.Stack;

//使用2个stack实现一个queue，以及queue的2个方法，队尾插入以及，队首删除。
public class seven_QueueWithTwoStacks<T> {

	private Stack<T> one = new Stack<T>();
	private Stack<T> two = new Stack<T>();

	public void appendTail(T t) {
		one.push(t);
	}

	public void deleteHead() {
		if (!two.isEmpty()) {
			two.pop();
		}else{
			while(!one.isEmpty()){
				T temp=one.pop();
				two.push(temp);
			}
			two.pop();
		}
	}
	public void show(){
		if(!one.isEmpty()){
		   System.out.print("head");
			for (T x : one) {
               System.out.print(x.toString()+" ");
		   } 
			System.out.println("tail");
		}
		if(!two.isEmpty()){
			   System.out.print("tail");
				for (T x : two) {
	               System.out.print(x.toString()+" ");
			   } 
				System.out.println("head");
			}
	}

	public static void main(String[] args) {
		seven_QueueWithTwoStacks<Integer> test = new seven_QueueWithTwoStacks<>();
		test.appendTail(1);
		test.appendTail(2);
		test.appendTail(3);
		test.appendTail(4);
		test.appendTail(5);
		test.appendTail(6);
		test.show();
		test.deleteHead();
		test.show();
	}
}
